package com.boolib.service.impl;

import com.boolib.commons.Const;
import com.boolib.commons.entity.ServerResponse;

import com.boolib.commons.util.ServerResponseUtil;

import com.boolib.dao.ShopBrandMapper;
import com.boolib.pojo.ShopBrand;
import com.boolib.pojo.ShopBrandExample;
import com.boolib.service.IBrandService;
import com.boolib.tree.TreeModelHandlerImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class BrandServiceImpl implements IBrandService {


    @Autowired
    private ShopBrandMapper shopBrandMapper;

    @Override
    public ServerResponse addBrand(ShopBrand shopBrand) {


        ServerResponse serverResponse =   ServerResponseUtil.argumentCheck(shopBrand.getBrandName(),shopBrand.getBrandLogo(),shopBrand.getCreateBy());

        if(!serverResponse.isSuccess()) return  serverResponse;

        ShopBrand shopBrand1 = new ShopBrand();


        shopBrand1.setBrandName(shopBrand.getBrandName());

        if(shopBrandMapper.select(shopBrand1).size()>0){
            return ServerResponse.createByError(Const.Brand.Status.UNEXIST.getCode(),
                    Const.Brand.Status.UNEXIST.getMessage());
        }


        Date date = new Date();
        shopBrand.setCreateTime(date);

        shopBrand.setUpdateTime(date);


        if(shopBrandMapper.insertSelective(shopBrand)>0) {
            log.info(shopBrand.getBrandName()+"品牌添加成功");
            return ServerResponse.createBySuccessMessage("品牌添加成功");
        }

        log.warn(shopBrand.getBrandName()+"品牌添加失败");

        return ServerResponse.createByErrorMessage("品牌添加失败");
    }


    @Override
    public ServerResponse forbidBrand(Integer id, String updateBy){
        ServerResponse serverResponse = ServerResponseUtil.argumentCheck(id,updateBy);
        if(!serverResponse.isSuccess())return serverResponse;

        ShopBrand shopBrand  = new ShopBrand();

        shopBrand.setId(id);

        ShopBrand result = shopBrandMapper.selectOne(shopBrand);
        serverResponse = ServerResponseUtil.resultCheck(result,ShopBrand.class);
        if(!serverResponse.isSuccess())return serverResponse;

        shopBrand.setStatus(Const.Brand.FORBID);
        shopBrand.setUpdateTime(new Date());
        shopBrand.setUpdateBy(updateBy);

        if(   shopBrandMapper.updateByPrimaryKeySelective(shopBrand)>0){
            ServerResponseUtil.updateSuccess("该品牌禁用成功");
        }

        return ServerResponseUtil.updateFail("该品牌禁用失败");

    }


    /**
     * 恢复品牌
     * @param id 品牌id
     * @param updateBy
     * @return
     */
    @Override
    public ServerResponse recoverBrand(Integer id, String updateBy) {

        ServerResponse serverResponse = ServerResponseUtil.argumentCheck(id,updateBy);

        if(!serverResponse.isSuccess()) return serverResponse;

        ShopBrand shopBrand = new ShopBrand();

        shopBrand.setId(id);

        ShopBrand result = shopBrandMapper.selectOne(shopBrand);

        serverResponse  =   ServerResponseUtil.resultCheck(result,ShopBrand.class);

        if(!serverResponse.isSuccess()) return serverResponse;

        shopBrand.setStatus(Const.Brand.NORMAL);

        shopBrand.setUpdateBy(updateBy);

        shopBrand.setUpdateTime(new Date());

        if(shopBrandMapper.updateByPrimaryKeySelective(shopBrand)>0){
            return ServerResponseUtil.updateSuccess("品牌状态恢复成功");
        }

        return ServerResponseUtil.updateFail("品牌状态恢复失败");
    }

    @Override
    public ServerResponse updateBrand(ShopBrand shopBrand, String updateBy) {

        ServerResponse serverResponse = ServerResponseUtil.argumentCheck(shopBrand.getId(),shopBrand.getBrandLogo(),shopBrand.getBrandName(),updateBy);

        if(!serverResponse.isSuccess())return serverResponse;

        ShopBrand shopBrand1 = new ShopBrand();
        shopBrand1.setId(shopBrand.getId());
        shopBrand1.setStatus(Const.Brand.NORMAL);
        ShopBrand result = shopBrandMapper.selectOne(shopBrand1);

        serverResponse = ServerResponseUtil.resultCheck(result,ShopBrand.class);

        if(!serverResponse.isSuccess()) return  serverResponse;


        shopBrand.setCreateTime(null);
        shopBrand.setCreateBy(null);
        shopBrand.setStatus(null);

        shopBrand.setUpdateBy(updateBy);
        shopBrand.setUpdateTime(new Date());

        if(shopBrandMapper.updateByPrimaryKeySelective(shopBrand)>0){
         return ServerResponseUtil.updateSuccess("品牌更新成功");
        }

        return ServerResponseUtil.updateFail("品牌更新失败");

    }

    @Override
    public ServerResponse selectBrandById(Integer id) {

        ServerResponse serverResponse = ServerResponseUtil.argumentCheck(id);
        if(!serverResponse.isSuccess()) return serverResponse;

        ShopBrand shopBrand = new ShopBrand();

        shopBrand.setId(id);
        shopBrand.setStatus(Const.Brand.NORMAL);
        ShopBrand result = shopBrandMapper.selectOne(shopBrand);

        return ServerResponse.createBySuccess(result);
    }

    @Override
    public ServerResponse selectBrandByName(String name) {
        ServerResponse serverResponse = ServerResponseUtil.argumentCheck(name);
        if(!serverResponse.isSuccess()) return serverResponse;

        ShopBrand shopBrand = new ShopBrand();

        shopBrand.setBrandName(name);
        shopBrand.setStatus(Const.Brand.NORMAL);
        ShopBrand result = shopBrandMapper.selectOne(shopBrand);

        return ServerResponse.createBySuccess(result);
    }


    @Override
    public ServerResponse selectAllNormal(int pageNum, int pageSize, boolean sort) {

        ShopBrandExample shopBrandExample = new ShopBrandExample();
        ShopBrandExample.Criteria criteria = shopBrandExample.createCriteria();

        criteria.andStatusEqualTo(0);



        if(sort == true){
            shopBrandExample.setOrderByClause("brandSort ASC");
        }
        else{
            shopBrandExample.setOrderByClause("createTime ASC");
        }
        List<ShopBrand> lists = shopBrandMapper.selectByExample(shopBrandExample);

        PageHelper.startPage(pageNum,pageSize);


        PageInfo pageInfo = new PageInfo();

        pageInfo.setList(lists);

        return  ServerResponse .createBySuccess(pageInfo);

    }

    @Override
    public ServerResponse selectAllAbnormal(int pageNum, int pageSize, boolean sort) {

        ShopBrandExample shopBrandExample = new ShopBrandExample();
        ShopBrandExample.Criteria criteria = shopBrandExample.createCriteria();

        criteria.andStatusGreaterThan(0);



        if(sort == true){
            shopBrandExample.setOrderByClause("brandSort ASC");
        }
        else{
            shopBrandExample.setOrderByClause("updateTime ASC");
        }
        List<ShopBrand> lists = shopBrandMapper.selectByExample(shopBrandExample);

        PageHelper.startPage(pageNum,pageSize);


        PageInfo pageInfo = new PageInfo(lists);



        return  ServerResponse .createBySuccess(pageInfo);
    }






}
